<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="weaver.soa.workflow.request.MainTableInfo" %>
<%@ page import="weaver.soa.workflow.request.Property" %>
<%@ page import="weaver.soa.workflow.request.RequestInfo" %>
<%@ page import="weaver.soa.workflow.request.RequestService" %>
<%@ page import="weaver.workflow.request.RequestComInfo" %>
<%@ page import="weaver.workflow.workflow.WorkflowComInfo" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="baseBean" class="weaver.general.BaseBean"/>
<%
    JSONObject jsonData = new JSONObject();
    try {
        String currentDate = DateUtil.getCurrentDate();
        baseBean.writeLog( ">>>>>>WeCycleGovernCronJob>>>>currentDate" + currentDate );
        String sql = "select * from uf_zqdc where  dcksrq<='" + currentDate + "' and dcjsrq >='" + currentDate + "'";
        rs.executeQuery( sql );
        jsonData.put("sql",sql);
        String [] columnName = rs.getColumnName();
        while (rs.next()) {
            Map<String, String> requestMainMap = new HashMap<>();
            //数据id
            String dataid = Util.null2String( rs.getString( "id" ) );
            jsonData.put("dataid",dataid);
            //督查重复模式:0-按天重复;1-按周重复;2-按月重复
            String dczfms = Util.null2String( rs.getString( "dczfms" ) );
            jsonData.put("dczfms",dczfms);
            //每次反馈期限（天）
            int mcfkqxt = Util.getIntValue( rs.getString( "mcfkqxt" ) );
            String xywcfkrq = DateUtil.addDate(DateUtil.addDate(currentDate,mcfkqxt),-1);
            String strs = getFieldName("-101");
            for (String column: columnName) {
                String columnValue = Util.null2String( rs.getString( column ) );
                if (!"".equals( columnValue ) && strs.contains( column )) {
                    requestMainMap.put( column, columnValue );
                }
            }
            requestMainMap.put("fkfqrq", currentDate );
            requestMainMap.put("sjid", dataid );
            requestMainMap.put("xywcfkrq", xywcfkrq );
            jsonData.put("requestMainMap",requestMainMap);
            switch (dczfms) {
                case "0":
                   runRntervalDay(currentDate,requestMainMap,dataid );
                    break;
                case "1":
                   runRntervalWeek( currentDate,requestMainMap,dataid );
                    break;
                case "2":
                   runRntervalMonth( currentDate,requestMainMap,dataid );
                    break;
                default:
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    out.print( jsonData.toJSONString() );
%>
<%!
    /**
     * @param @param  creatorId    创建人Id
     * @param @param  workflowId   流程Id
     * @param @param  requestName  请求标题
     * @param @param  IsNextFlow   是否提交到下一节点
     * @param @param  requestMap   Map格式的主表数据<fieldname,fieldvalue>=<字段名称,字段值>
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: WorkflowCreateByMainTableMap
     */
    public static String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( "-----创建流程传递的参数个数--------" );
        baseBean.writeLog( "creatorId:" + creatorId );
        baseBean.writeLog( "workflowId:" + workflowId );
        baseBean.writeLog( "requestName:" + requestName );
        baseBean.writeLog( "IsNextFlow:" + IsNextFlow );

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid( creatorId );//创建人Id
        requestInfo.setWorkflowid( workflowId );//工作流Id
        requestInfo.setDescription( requestName );//请求标题
        if ("0".equals( IsNextFlow )) {
            requestInfo.setIsNextFlow( IsNextFlow );
        }

        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName( Util.null2String( entry.getKey() ) );
            propertyArray[p].setValue( Util.null2String( entry.getValue() ) );
            baseBean.writeLog( "\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value=" + entry.getValue() );
            p++;
        }
        baseBean.writeLog( "-----创建流程传递的参数个数p=" + p );
        mainTableInfo.setProperty( propertyArray );
        requestInfo.setMainTableInfo( mainTableInfo );
        RequestService service = new RequestService();
        try {
            //流程
            try {
                requestid = service.createRequest( requestInfo );//创建请求id
            } catch (Exception e) {
                baseBean.writeLog( "=======" + e.getMessage() );
            }
            //String userId = requestInfo.getLastoperator();//请求最后的操作者
            //记录日志
            StringBuffer sbf = new StringBuffer( "\r\n-----xwd创建工作流记录日志开始" );
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append( "\r\n-----姓名:" + rscif.getLastname( rcif.getRequestCreater( requestid ) ) );
            sbf.append( "\r\n-----时间:" + rcif.getRequestCreateTime( requestid ) );
            sbf.append( "\r\n-----创建流程:" + wfcif.getWorkflowname( workflowId ) );
            sbf.append( "\r\n-----请求:" + rcif.getRequestname( requestid ) );
            sbf.append( "\r\n-----请求:" + requestid );
            sbf.append( "\r\n-----创建工作流记录日志结束" );
            baseBean.writeLog( sbf.toString() );
        } catch (Exception e) {
            baseBean.writeLog( "错误：" + e );
        }
        return requestid;
    }
    /**
     *@Description: 间隔天数发起
     *@Param: [days]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    public static boolean runRntervalDay(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        String dcrq = Util.null2String(requestMainMap.get( "dcrq" ));
        int jgts = Util.getIntValue(requestMainMap.get( "jgts" ));
        String zrr = Util.null2String(requestMainMap.get( "zrr" ));
        String xywcfkrq = Util.null2String(requestMainMap.get( "xywcfkrq" ));
        int newDays =  DateUtil.dayDiff(currentDate,dcrq);
        if(newDays%jgts==0){
            String requestid = createWorkflow(zrr,currentDate,requestMainMap);
            if(Util.getIntValue( requestid )>0){
                RecordSet rs = new RecordSet(  );
                String sql = "insert into uf_zqdc_dt1(mainid,fklc,fkfqrq,xywcfkrq) values(?,?,?,?)";
                flag =  rs.executeUpdate(sql,new Object[]{dataid,requestid,currentDate,xywcfkrq});
            }
        }

        return flag;
    }

    /**
     *@Description: 间隔周数发起
     *@Param: [weeks,weekday]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    public static boolean runRntervalWeek(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog(">>>>>runRntervalWeek>>>>");
        String dcrq = Util.null2String(requestMainMap.get( "dcrq" ));
        int jgzs = Util.getIntValue(requestMainMap.get( "jgzs" ));
        int mzjfqfk = Util.getIntValue(requestMainMap.get( "mzjfqfk" ));
        String zrr = Util.null2String(requestMainMap.get( "zrr" ));
        String xywcfkrq = Util.null2String(requestMainMap.get( "xywcfkrq" ));
        baseBean.writeLog("dcrq="+dcrq+",jgzs="+jgzs+",mzjfqfk="+mzjfqfk+",zrr="+zrr+",xywcfkrq="+xywcfkrq);
        Date curdate = DateUtil.parseToDate(currentDate);
        int curweek = DateUtil.getWeekOfYear(curdate);
        Date governdate = DateUtil.parseToDate(dcrq);
        int governweek = DateUtil.getWeekOfYear(governdate);
        baseBean.writeLog("curweek="+curweek+",governweek="+governweek);
        if((curweek-governweek)%jgzs==0) {
            int newweekday = DateUtil.getWeek( currentDate );
            baseBean.writeLog("newweekday="+newweekday);
            //if (newweekday == mzjfqfk) {
                String requestid = createWorkflow(zrr,currentDate,requestMainMap);
                if(Util.getIntValue( requestid )>0){
                    RecordSet rs = new RecordSet(  );
                    String sql = "insert into uf_zqdc_dt1(mainid,fklc,fkfqrq,xywcfkrq) values(?,?,?,?)";
                    flag =  rs.executeUpdate(sql,new Object[]{dataid,requestid,currentDate,xywcfkrq});
                }
           // }
        }
        return flag;
    }
    /**
     *@Description: 间隔月数发起
     *@Param: [months,date]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    public static boolean runRntervalMonth(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        String dcrq = Util.null2String(requestMainMap.get( "dcrq" ));
        int jgys = Util.getIntValue(requestMainMap.get( "jgys" ));
        int mydjtfqfk = Util.getIntValue(requestMainMap.get( "mydjtfqfk" ));
        String zrr = Util.null2String(requestMainMap.get( "zrr" ));
        String xywcfkrq = Util.null2String(requestMainMap.get( "xywcfkrq" ));
        int curMonth = Util.getIntValue(DateUtil.getMonth());
        Calendar calendar = DateUtil.getCalendar(dcrq);
        int governMonth = Util.getIntValue(DateUtil.getMonth( calendar ));
        if((curMonth-governMonth)%jgys==0) {
            int newdate = Util.getIntValue(currentDate.substring( 8,10 ));
            if(newdate == mydjtfqfk){
                String requestid = createWorkflow(zrr,currentDate,requestMainMap);
                if(Util.getIntValue( requestid )>0){
                    RecordSet rs = new RecordSet(  );
                    String sql = "insert into uf_zqdc_dt1(mainid,fklc,fkfqrq,xywcfkrq) values(?,?,?,?)";
                    flag = rs.executeUpdate(sql,new Object[]{dataid,requestid,currentDate,xywcfkrq});
                }
            }
        }
        return flag;
    }
    /**
     *@Description: 创建流程
     *@Param: [userid, curdate, requestMainMap]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    private static String createWorkflow(String userid,String curdate, Map<String, String> requestMainMap){
        String workflowId = "56";
        String workflowName = "周期督查反馈";
        String isNextFlow = "1";
        String requestid = "";
        try {
            ResourceComInfo rci = new ResourceComInfo();
            String requestName = workflowName + "-" + rci.getLastname() + "-" + curdate;
            requestid =  WorkflowCreateByRequestMap( userid, workflowId, requestName, isNextFlow, requestMainMap );
        }catch (Exception e){
            e.printStackTrace();
        }
        return requestid;
    }
    /**
     *@Description: 获取流程表单字段
     *@Param: [formid, tablename]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    public static String  getFieldName(String formid){
        RecordSet rs = new RecordSet(  );
        String strs = "";
        rs.executeQuery( "select fieldname from workflow_billfield where billid=? and viewtype=0", formid );
        while (rs.next()) {
            String fieldname = Util.null2String( rs.getString( "fieldname" ) );
            strs +="".equals( fieldname )?fieldname:","+fieldname;
        }
        return strs;
    }
%>